﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Demo.Mmose.Core.Common;
using Demo.Mmose.Core.Util;
using Demo.Mmose.Core.Common.Atom;
using System.Collections.Concurrent;
#endregion

namespace Demo.Mmose.Core.World
{
    /// <summary>
    /// 一般用于SQL的回调
    /// </summary>
    public class WorldWaitExecute
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        internal WorldWaitExecute( WorldBase world )
        {
            if ( world == null )
                throw new ArgumentNullException( "world", "WorldWaitExecute.WorldWaitExecute(...) - world == null error!" );

            m_World = world;
        }
        #endregion

        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS World属性 | en World Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 管理当前的世界服务
        /// </summary>
        private WorldBase m_World = null;
        #endregion
        /// <summary>
        /// 管理当前的世界服务
        /// </summary>
        public WorldBase World
        {
            get { return m_World; }
        }

        #endregion

        #endregion

        #region zh-CHS 内部方法 | en Internal Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 是否在调用Slice(...)中...的锁
        /// </summary>
        private LockInOut m_LockInSliceWaitExecute = new LockInOut( false );
        #endregion
        /// <summary>
        /// 
        /// </summary>
        internal void Slice()
        {
            // 检查是否已经在处理Slice(...)的调用
            if ( m_LockInSliceWaitExecute.InLock() == false )
                return;

            IWaitExecuteInfo waitExecuteInfo = null;
            do
            {
                waitExecuteInfo = null;

                if ( m_WaitExecuteQueueHighest.TryDequeue( out waitExecuteInfo ) == false )
                    if ( m_WaitExecuteQueueNormal.TryDequeue( out waitExecuteInfo ) == false )
                        if ( m_WaitExecuteQueueLowest.TryDequeue( out waitExecuteInfo ) == false )
                            break;

                if ( waitExecuteInfo != null )
                    waitExecuteInfo.Execute();

            } while ( waitExecuteInfo != null );// 可能执行的时间比较长 所以再检测一下是不是还有

            // 已经处理完Slice(...)的调用
            m_LockInSliceWaitExecute.OutLock();
        }

        #endregion

        #region zh-CHS 共有方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 当前需要处理的集合（低优先级）
        /// </summary>
        private ConcurrentQueue<IWaitExecuteInfo> m_WaitExecuteQueueLowest = new ConcurrentQueue<IWaitExecuteInfo>();
        /// <summary>
        /// 当前需要处理的集合（中优先级）
        /// </summary>
        private ConcurrentQueue<IWaitExecuteInfo> m_WaitExecuteQueueNormal = new ConcurrentQueue<IWaitExecuteInfo>();
        /// <summary>
        /// 当前需要处理的集合（高优先级）
        /// </summary>
        private ConcurrentQueue<IWaitExecuteInfo> m_WaitExecuteQueueHighest = new ConcurrentQueue<IWaitExecuteInfo>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="waitCallBack"></param>
        [MultiThreadedWarning( "zh-CHS", "此处IWaitExecuteInfo的回调只会同步在一个线程中处理，即处理完成以后才会再次处理回调:警告!" )]
        public void JoinWaitExecuteQueue( IWaitExecuteInfo waitExecuteInfo )
        {
            this.JoinWaitExecuteQueue( WaitExecutePriority.Normal, waitExecuteInfo );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="waitCallBack"></param>
        [MultiThreadedWarning( "zh-CHS", "此处IWaitExecuteInfo的回调只会同步在一个线程中处理，即处理完成以后才会再次处理回调:警告!" )]
        public void JoinWaitExecuteQueue( WaitExecutePriority waitExecutePriority, IWaitExecuteInfo waitExecuteInfo )
        {
            if ( m_World == null )
                throw new ArgumentNullException( "world", "WorldWaitExecute.JoinWaitExecuteQueue(...) - m_World == null error!" );

            if ( waitExecuteInfo == null )
                throw new ArgumentNullException( "waitExecuteInfo", "WorldWaitExecute.JoinWaitExecuteQueue(...) - waitExecuteInfo == null error!" );

            if ( waitExecutePriority == WaitExecutePriority.Normal )
                m_WaitExecuteQueueNormal.Enqueue( waitExecuteInfo );
            else if ( waitExecutePriority == WaitExecutePriority.Highest )
                m_WaitExecuteQueueHighest.Enqueue( waitExecuteInfo );
            else if ( waitExecutePriority == WaitExecutePriority.Lowest )
                m_WaitExecuteQueueLowest.Enqueue( waitExecuteInfo );

            m_World.SendWorldSignal();
        }

        #endregion
    }
}
#endregion